package org.shangrila.bme.mb;

/**
 * 使用JSF Bean： new DbAccess()
 *  
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmeMaterial;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.IDHelper;
 
@Named
@SessionScoped
public class MaterialMB implements Serializable {
	private static final long serialVersionUID = -5951492023664217106L;
	private List<BmeMaterial> vo = new ArrayList<BmeMaterial>(); //显示列表
    private BmeMaterial io;
    private BmeMaterial selected;

    //获取相关数据字典
	private List<SelectItem> itemsMsort = AppInitInfo.getDropdownList("Msort","NONE"); //NONE-ALL-NULL
	private List<SelectItem> itemsMtype = AppInitInfo.getDropdownList("Mtype"); //NONE

	@Inject
	private DbAccess db;
    
    @PostConstruct
    public void init() {
    	newIo();    	
    }
         
    public void newIo() {
    	io = new BmeMaterial();
    	io.setId(IDHelper.genStrID(20));
    }   
    
	public String addAction() {
		
		db.beginTransaction();
		db.insert(io);
		db.commitTransaction();
		
		showMessage("录入", "信息录入成功");
		
		vo.add(io);
		newIo();
		return null;
	}
    
	public String queryAction() {		
		vo = db.findEntityAll("BmeMaterial");
		return null;
	}

    public void onRowEdit(RowEditEvent event) {
    	selected = vo.get(vo.indexOf((BmeMaterial) event.getObject()));
    	db.beginTransaction();
    	db.update(selected);
    	db.commitTransaction();
    	showMessage("编辑", "编辑保存成功。");
    }
     
    public void onRowCancel(RowEditEvent event) {
    	showMessage("编辑", "取消编辑成功。");
    }
    
	public void showMessage(String summary, String detail) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public List<BmeMaterial> getVo() {
		return vo;
	}

	public void setVo(List<BmeMaterial> vo) {
		this.vo = vo;
	}

	public BmeMaterial getIo() {
		return io;
	}

	public void setIo(BmeMaterial io) {
		this.io = io;
	}

	public BmeMaterial getSelected() {
		return selected;
	}

	public void setSelected(BmeMaterial selected) {
		this.selected = selected;
	}

	public List<SelectItem> getItemsMsort() {
		return itemsMsort;
	}

	public void setItemsMsort(List<SelectItem> itemsMsort) {
		this.itemsMsort = itemsMsort;
	}

	public List<SelectItem> getItemsMtype() {
		return itemsMtype;
	}

	public void setItemsMtype(List<SelectItem> itemsMtype) {
		this.itemsMtype = itemsMtype;
	}

 
}